﻿using System;
using System.Collections.Generic;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Linq;

namespace Flare.DB
{
    class equipment_models
    {
        public IEnumerable<CeEntity.equipment_model> Index(int? id_mark = null)
        {
            var result = from m in Source.equipment_model()
                         where m.id_mark == id_mark
                         select m;
            return result;
        }

        public IEnumerable<Entity.EquipmentModelAdvance> Index2(int? id_type = null, int? id_mark = null)
        {
            var result = from ml in Source.equipment_model()
                         join m in Source.equipment_mark() on ml.id_mark equals m.id_mark
                         join t in Source.equipment_type() on m.id_type equals t.id_type
                         where (t.id_type == id_type || id_type == null) &&
                         (m.id_mark == id_mark || id_mark == null)
                         
                         select new Entity.EquipmentModelAdvance { 
                            id_mark = m.id_mark,
                            id_model = ml.id_model,
                            id_type = t.id_type,
                            model = ml.model
                         };
            return result;
        }

        public void Create(Entity.EquipmentModel data)
        {
            data.id_model = 0;
            var validate = data.Validate();
            if (validate.Count() != 0)
            {
                throw new Exception(string.Join<string>("\n", validate));
            }

            using (SqlCeConnection connection = new SqlCeConnection(glob.DBConnection))
            {
                using (SqlCeCommand command = new SqlCeCommand(string.Format("INSERT INTO [equipment_model] ([model], [id_mark]) VALUES ('{0}',{1})", data.model, data.id_mark), connection))
                {
                   	try
                    {
                        connection.Open();
                    }
                    catch (Exception ex)
                    {
                        glob.kernel.Panic(glob.PanicType.db_connect, ex);
                    }

                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(command.CommandText + "\n" +ex.Message);
                        throw new Exception("Во время добавления данных произошла ошибка. Данные не сохранены");
                    }
                }
            }
        }

        public void Edit(Entity.EquipmentModel data)
        {
            var validate = data.Validate();
            if (validate.Count() != 0)
            {
                throw new Exception(string.Join<string>("\n", validate));
            }

            using (SqlCeConnection connection = new SqlCeConnection(glob.DBConnection))
            {
                using (SqlCeCommand command = new SqlCeCommand(
                        string.Format("UPDATE [equipment_model] SET [model] = '{1}', [id_mark] = {2} WHERE [id_model]= {0}", data.id_model, data.model, data.id_mark), connection))
                {
                   	try
                    {
                        connection.Open();
                    }
                    catch (Exception ex)
                    {
                        glob.kernel.Panic(glob.PanicType.db_connect, ex);
                    }

                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(command.CommandText + "\n" +ex.Message);
                        throw new Exception("Во время обновления данных произошла ошибка. Данные не сохранены");
                    }
                }
            }
        }

        public void Delete(int id_model)
        {
            using (SqlCeConnection connection = new SqlCeConnection(glob.DBConnection))
            {
                using (SqlCeCommand command = new SqlCeCommand(
                    string.Format("DELETE FROM [equipment_model] WHERE [id_model] = {0}", id_model), connection))
                {
                 	try
                    {
                        connection.Open();
                    }
                    catch (Exception ex)
                    {
                        glob.kernel.Panic(glob.PanicType.db_connect, ex);
                    }

                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(command.CommandText + "\n" +ex.Message);
                        throw new Exception("Во время удаления данных произошла ошибка. Данные не сохранены");
                    }
                }
            }
        }
    }
}